package han.study.dao;

import han.study.utils.JDBCUtils;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseDao {

    // 通用增删改操作---version 2.0(考虑事务)
    public int update(Connection conn, String sql, Object... args){
        PreparedStatement ps = null;
        try{
            ps = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            return ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(null,ps);
        }
        return 0;
    }
    // 通用查询操作，用于返回数据表中的一条记录 2.0(考虑事务)
    public <T> T getInstance(Connection conn, Class<T> clazz,String sql, Object... args){
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            ps = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            if(rs.next()){
                T t = clazz.getDeclaredConstructor().newInstance();
                for(int i=1;i<=columnCount;i++){
                    String columnLabel = metaData.getColumnLabel(i);
                    Object columnValue = rs.getObject(i);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,columnValue);
                }
                return t;
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(null,ps,rs);
        }
        return null;
    }
    // 通用查询操作，用于批量返回查询结果 2.0 考虑事务
    public <T> List<T> getForList(Connection conn, Class<T> clazz,String sql,Object... args){
        PreparedStatement ps = null;
        ResultSet rs =  null;
        List<T> list = null;
        try{
            ps = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            while(rs.next()){
                T t = clazz.getDeclaredConstructor().newInstance();
                for(int i=1;i<=columnCount;i++){
                    String columnLabel = metaData.getColumnLabel(i);
                    Object columnValue = rs.getObject(i);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,columnValue);
                }
                if (list == null) {
                    list = new ArrayList<T>();
                }
                list.add(t);
            }
            return list;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(null,ps,rs);
        }
        return null;
    }
    // 通用特殊一列操作 例如 select count(*) from user_table
    public <E> E getValue(Connection conn,String sql, Object... args){
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            ps = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1, args[i]);
            }
            rs = ps.executeQuery();
            if(rs.next()){
                return (E) rs.getObject(1);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(null,ps,rs);
        }
        return null;
    }
}
